﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
using ComLib.ComFun;
using System.Reflection;

namespace KProcessor
{
    public class PhotoErrorCounter
    {       
        private Hashtable _ht = new Hashtable();
      
        public PhotoErrorCounter()
        {
            InitData();
        }

        private void InitData()
        {
            List<string> listErrors = ReadPhotoErrors();
            foreach(string key in listErrors)
            {
                _ht.Add(key, 0);
            }
        }

        public void Reset()
        {
            Hashtable htNew = new Hashtable();
            foreach (DictionaryEntry dicEntry in _ht)
            {
                htNew.Add(dicEntry.Key, 0);
            }
            _ht.Clear();
            _ht = null;
            _ht = htNew;
        }

        public bool IsEmpty
        {
            get
            {
                return _ht.Count == 0;
            }
        }
       
        private List<string> ReadPhotoErrors()
        {
            string directory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
            directory = directory.Substring(6);
            string filePath = Path.Combine(directory, "PhotoErrors.txt");
            return IPCCom.ReadTextLines(filePath);
        }

        public DictionaryEntry GetMaxCountError()
        {
            if (_ht.Count > 0)
            {
                ArrayList alist = new ArrayList(_ht.Values);
                alist.Sort();
                object count = alist[alist.Count - 1];
                int x = (int)count;
                foreach (DictionaryEntry dicEntry in _ht)
                {
                    if (dicEntry.Value == count)
                    {
                        return dicEntry;
                    }
                }       
            }              
            return new DictionaryEntry();
        }

        public bool IsOverRangle(int max, string key)
        {
            if (_ht.ContainsKey(key))
            {
                object count = _ht[key];
                int x = (int)count;
                if (x > max)
                {
                    return true;
                }              
            }              
            return false;
        }

        public void AddKey(string key)
        {           
            if (_ht.ContainsKey(key))
            {
                int i = (int)_ht[key];
                _ht.Remove(key);
                _ht.Add(key, ++i);
            }
            else
            {
                _ht.Add(key, 1);
            }
        }
    }
}
